package com.desuntech.utils
{
	public class MenuHelper
	{
		public static function buildMenuAll(result:Array):XML {
			result.sortOn(['SJBM', 'BJBM']);
			if(result.length) {
				var menuXML:XML = new XML('<menu />');
				var i:int;
				for(i=0;i<result.length;i++) {
					if(result[i]['SJBM'].length == 2) {
						var node:XML = new XML('<node />');
						node.@formid = result[i]['formid'];
						node.@label = result[i]['CDMC'];
						node.@CDBH = result[i]['CDBH'];
						node.@type = '';
						menuXML.appendChild(node);
					}
				}
				for(i=0;i<result.length;i++) {
					var sjbm:String = result[i]['SJBM'];
					if(sjbm.length == 5) {
						var node:XML = new XML('<node />');
						node.@formid = result[i]['formid'];
						node.@label = result[i]['CDMC'];
						node.@CDBH = result[i]['CDBH'];
						if(result[i]['hasChild']) {
							node.@type = '';
						} else {
							node.@name = result[i]['CDBH'];
							node.@type = 'view';
							node.@selected = false;
							node.@id = result[i]['formid'];
							switch(result[i]['CDBH']) {
								case AppConstants.VIEW_JCJG:
								case AppConstants.VIEW_ZJZ:
								case AppConstants.VIEW_JCRY:
								case AppConstants.VIEW_GCXX:
								case AppConstants.VIEW_JCBG:
								case AppConstants.VIEW_CJSJ:
								case AppConstants.VIEW_XGJL:
								case AppConstants.VIEW_JZRY:
								case AppConstants.VIEW_JZJL:
								case AppConstants.VIEW_JCHT:
								case AppConstants.VIEW_JCFA:
								case AppConstants.VIEW_CXTJ:
									node.@module_name = AppConstants.JCJG;
									node.@module_url = 'com/desuntech/modules/JcjgModule/JcjgModule.swf';
									break;
							}
						}
						var parent:XML = menuXML.node.(attribute('CDBH') == sjbm)[0];
						parent.appendChild(node);
					}
				}
				for(i=0;i<result.length;i++) {
					var sjbm:String = result[i]['SJBM'];
					if(sjbm.length == 7) {
						var node:XML = new XML('<node />');
						node.@formid = result[i]['formid'];
						node.@label = result[i]['CDMC'];
						node.@CDBH = result[i]['CDBH'];
						if(result[i]['hasChild']) {
							node.@type = '';
						} else {
							node.@name = result[i]['CDBH'];
							node.@type = 'view';
							node.@selected = false;
							node.@id = result[i]['formid'];
							switch(result[i]['CDBH']) {
								case AppConstants.VIEW_JCXMDYBZ:
								case AppConstants.VIEW_JCCSDYBZ:
								case AppConstants.VIEW_JCXM:
								case AppConstants.VIEW_JCCS:
								case AppConstants.VIEW_BZK:
								case AppConstants.VIEW_JSDYQX:
									node.@module_name = AppConstants.JCJG;
									node.@module_url = 'com/desuntech/modules/JcjgModule/JcjgModule.swf';
									break;
							}
						}
						var parent:XML = menuXML..node.(attribute('CDBH') == sjbm)[0];
						parent.appendChild(node);
					}
				}
				return menuXML;
			} else {
				return null;
			}
		}
		
		public static function buildMenuForRole(menuAll:XML, formsForRole:Array):XML {
			var menuForRole:XML = menuAll.copy();
			
			var i:int = 0;
			var lenI:int = menuForRole.node.length();
			while(i<lenI) {
				if(menuForRole.node[i].@type == 'view') {
					var matchI:Boolean = false;
					for(var ii:int=0;ii<formsForRole.length;ii++) {
						if(menuForRole.node[i].@formid == formsForRole[ii]['formid']) {
							matchI = true;
							i++;
							break;
						}
					}
					if(!matchI) {
						delete menuForRole.node[i];
						lenI = menuForRole.node.length();
					}
				} else if(menuForRole.node[i].node.length()){
					var j:int = 0;
					var lenJ:int = menuForRole.node[i].node.length();
					while(j<lenJ) {
						if(menuForRole.node[i].node[j].@type == 'view') {
							var matchJ:Boolean = false;
							for(var jj:int=0;jj<formsForRole.length;jj++) {
								if(menuForRole.node[i].node[j].@formid == formsForRole[jj]['formid']) {
									matchJ = true;
									j++;
									break;
								}
							}
							if(!matchJ) {
								delete menuForRole.node[i].node[j];
								lenJ = menuForRole.node[i].node.length();
							}
						} else if(menuForRole.node[i].node[j].node.length()) {
							var k:int = 0;
							var lenK:int = menuForRole.node[i].node[j].node.length();
							while(k<lenK) {
								if(menuForRole.node[i].node[j].node[k].@type == 'view') {
									var matchK:Boolean = false;
									for(var kk:int=0;kk<formsForRole.length;kk++) {
										if(menuForRole.node[i].node[j].node[k].@formid == formsForRole[kk]['formid']) {
											matchK = true;
											k++;
											break;
										}
									}
									if(!matchK) {
										delete menuForRole.node[i].node[j].node[k];
										lenK = menuForRole.node[i].node[j].node.length();
									}
								}
							}
							j++;
						}
					}
					i++;
				}
			}
			
			for(var l:int=0;l<2;l++) {
				var i:int = 0;
				var lenI:int = menuForRole.node.length();
				while(i<lenI) {
					if(menuForRole.node[i].@type == '' && menuForRole.node[i].node.length() == 0) {
						delete menuForRole.node[i];
						lenI = menuForRole.node.length();
					} else if(menuForRole.node[i].node.length()){
						var j:int = 0;
						var lenJ:int = menuForRole.node[i].node.length();
						while(j<lenJ) {
							if(menuForRole.node[i].node[j].@type == '' && menuForRole.node[i].node[j].node.length() == 0) {
								delete menuForRole.node[i].node[j];
								lenJ = menuForRole.node[i].node.length();
							} else if(menuForRole.node[i].node[j].node.length()) {
								var k:int = 0;
								var lenK:int = menuForRole.node[i].node[j].node.length();
								while(k<lenK) {
									if(menuForRole.node[i].node[j].node[k].@type == '' && menuForRole.node[i].node[j].node[k].node.length() == 0) {
										delete menuForRole.node[i].node[j].node[k];
										lenK = menuForRole.node[i].node.length();
									} else {
										k++;
									}
								}
								j++;
							} else if(menuForRole.node[i].node[j].@type != '') {
								j++;
							}
						}
						i++;
					} else if(menuForRole.node[i].@type != '') {
						i++;
					}
				}
			}
			
			return menuForRole;
		}
		
		public static function tagMenuAllByRole(menuAll:XML, formsForRole:Array):void {
			var nodes:XMLList = menuAll..node.(@type == 'view');
			for(var i:int=0;i<nodes.length();i++) {
				nodes[i].@selected = false;
				for(var j:int=0;j<formsForRole.length;j++) {
					if(nodes[i].@formid == formsForRole[j]['formid']) {
						nodes[i].@selected = true;
						break;
					}
				}
			}
		}
	}
}